//
//  SQAcceptanceViewController.m
//  energy
//
//  Created by 周曼迪 on 2021/7/18.
//

#import "SQAcceptanceLookViewController.h"

#import "SQQueryModel.h"
#import "SQAcceptanceLookModel.h"

#import "NSString+NCDate.h"
#import "SQPlayVoiceView.h"
#import "SQShowImage.h"
#import "AppDelegate+Tool.h"
@interface SQAcceptanceLookViewController ()<BaseTableVCInterface>
@property (weak, nonatomic) IBOutlet UIView *view_acceptanceBtn;

@property (strong, nonatomic)SQQueryModel *queryModel;
/**播放录音*/
@property (strong, nonatomic) SQPlayVoiceView * playVoiceView;
@property (strong, nonatomic) SQPlayVoiceView * playVoiceinformationView;
@property (weak, nonatomic) IBOutlet UIView *view_acceptanceVoice;
@property (weak, nonatomic) IBOutlet UIView *view_informationVoice;

@property (weak, nonatomic) IBOutlet UIView *view_acceptance;
@property (weak, nonatomic) IBOutlet UITextField *tf_acceptanceDepartment;//整改人部门
@property (weak, nonatomic) IBOutlet UIButton *btn_acceptance;
@property (weak, nonatomic) IBOutlet UITextField *tf_acceptancepeople;//整改人

@property (weak, nonatomic) IBOutlet UITextField *tf_acceptanceRectification;//整改情况

@property (weak, nonatomic) IBOutlet UITextField *tf_completionTime;//计划完成时间

@property (weak, nonatomic) IBOutlet UITextField *tf_acceptanceActualTime;//实际整改时间

@property (weak, nonatomic) IBOutlet UITextField *tf_money;//整改资金
@property (weak, nonatomic) IBOutlet UITextField *tf_prevention;//防控措施

@property (weak, nonatomic) IBOutlet UITextField *tf_system;//制度措施清单
@property (weak, nonatomic) IBOutlet UITextField *tf_rectification;//整改措施

@property (weak, nonatomic) IBOutlet UIImageView *img_picAcceptanceOne;
@property (weak, nonatomic) IBOutlet UIImageView *img_picAcceptanceTwo;
@property (weak, nonatomic) IBOutlet UIImageView *img_picAcceptanceThree;

@property (weak, nonatomic) IBOutlet UITextView *tv_describe;//隐患描述
@property (weak, nonatomic) IBOutlet UITextField *tf_type;//隐患类别
@property (weak, nonatomic) IBOutlet UITextField *tf_level;//隐患等级
@property (weak, nonatomic) IBOutlet UITextField *tf_rectificationType;//隐患整改类型

@property (weak, nonatomic) IBOutlet UITextView *tv_note;//备注
@property (weak, nonatomic) IBOutlet UIView *view_information;
@property (weak, nonatomic) IBOutlet UIButton *btn_information;

@property (weak, nonatomic) IBOutlet UITextField *tf_area;//区域
@property (weak, nonatomic) IBOutlet UITextField *tf_people;//发现人

@property (weak, nonatomic) IBOutlet UITextField *tf_class;//班组

@property (weak, nonatomic) IBOutlet UITextField *tf_assessmentTime;//评估时间


@property (weak, nonatomic) IBOutlet UIImageView *img_picOne;
@property (weak, nonatomic) IBOutlet UIImageView *img_picTwo;
@property (weak, nonatomic) IBOutlet UIImageView *img_picThree;


@property (strong, nonatomic) IBOutlet UIView *view_popupWindow;



@end

@implementation SQAcceptanceLookViewController

- (void)viewDidLoad {
    
    self.tableViewInterface = self;
    [super viewDidLoad];
    self.navigationItem.title = @"隐患验收";
    self.view_acceptance.hidden = NO;
    self.view_information.hidden = YES;
    
    self.playVoiceView = [[SQPlayVoiceView alloc] init];
    
    [self.view_acceptanceVoice addSubview:self.playVoiceView];
    
    self.playVoiceinformationView = [[SQPlayVoiceView alloc] init];
    
    [self.view_informationVoice addSubview:self.playVoiceinformationView];
  
    
    if ([self.type isEqualToString:@"2"]) {
        self.view_acceptanceBtn.hidden = NO;
        self.tv_describe.editable = YES;
    }else if([self.type isEqualToString:@"1"]){
        self.view_acceptanceBtn.hidden = YES;
        self.tv_describe.editable = NO;
    }
    
    [self getAcceptanceLookData];
}

-(void)getAcceptanceLookData{
    [SQQueryModel getDataWithParams:@{@"hd_id":self.hd_id} WithCallBack:^(SQQueryModel * data) {
        self.queryModel = data;
        self.tf_acceptanceDepartment.text = data.data.hd_rectification_company_text;
        self.tf_acceptancepeople.text = data.data.hd_rectification_person_text;
        self.tf_acceptanceRectification.text = data.data.hd_description_of_rectification;
        self.tf_rectificationType.text = data.data.hd_rectification_type_text;
        self.tf_acceptanceActualTime.text = data.data.hd_rectification_time;
//        self.tv_describe.text = data.data.hd_danger_description;
        
        self.tf_area.text = data.data.site_area;
        self.tv_note.text = data.data.hd_danger_description;
        self.tf_people.text = data.data.hd_report_person_text;
        self.tf_class.text = data.data.hd_confirm_team_person_text;
//        self.tf_department.text = data.data.hd_confirm_department_person_text;
//        self.tf_company.text = data.data.hd_confirm_company_person_text;
        self.tf_assessmentTime.text = [NSString formateDate:[data.data.hd_report_time substringWithRange:NSMakeRange(6,13)]];
        
        self.tf_type.text = data.data.hd_category_text;
        self.tf_level.text = data.data.hd_level_text;
        self.tf_rectificationType.text = data.data.hd_rectification_type_text;
        self.tf_completionTime.text = data.data.hd_plan_complete;
        self.tf_money.text = data.data.hd_rectification_funds_text;
        self.tf_prevention.text = data.data.hd_prevention_and_control_measures;
        self.tf_system.text = data.data.hd_list_of_system_measures;
        self.tf_rectification.text = data.data.hd_rectification_measures;
        
        
        
        
        [self.img_picOne setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",LC_ReqHeader,data.data.hd_report_pic_one]] placeholder:[UIImage imageNamed:@"addImage"]];
        
        [self.img_picTwo setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",LC_ReqHeader,data.data.hd_report_pic_two]] placeholder:[UIImage imageNamed:@"addImage"]];
        
        [self.img_picThree setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",LC_ReqHeader,data.data.hd_report_pic_three]] placeholder:[UIImage imageNamed:@"addImage"]];
        
        [self.playVoiceinformationView buildPlayVoiceVie:data.data.hd_report_pic_audio];
        
        [self.playVoiceView buildPlayVoiceVie:data.data.hd_rectification_pic_audio];
        
        [self.img_picAcceptanceOne setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",LC_ReqHeader,data.data.hd_rectification_pic_one]] placeholder:[UIImage imageNamed:@"addImage"]];
        
        [self.img_picAcceptanceTwo setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",LC_ReqHeader,data.data.hd_rectification_pic_two]] placeholder:[UIImage imageNamed:@"addImage"]];
        
        [self.img_picAcceptanceThree setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",LC_ReqHeader,data.data.hd_rectification_pic_three]] placeholder:[UIImage imageNamed:@"addImage"]];
    
    
    }];
}

//༥༤༽ཀབཀྲ་ཤིས་བདེ་ལེགས།


-(NSInteger)rowsCount:(NSInteger)section{
    return 0;
}


- (NSArray<NSArray<NSString *> *> *)classArr{
    return @[@[@"SQConfirmTableViewCell"]];
}



//༥༤༽ཀབཀྲ་ཤིས་བདེ་ལེགས།

/**隐患验收
*/
- (IBAction)clickConfirm:(UIButton *)sender {
    
    [self.tableView beginUpdates];
    self.tableView.tableHeaderView.height = W(852);
    [self.tableView  endUpdates];
    
    [self.btn_acceptance setTitleColor:[UIColor colorNamed:@"AccentColor"] forState:UIControlStateNormal];
    [self.btn_information setTitleColor:[UIColor colorNamed:@"333Color"] forState:UIControlStateNormal];
    self.view_acceptance.hidden = NO;
    self.view_information.hidden = YES;
    if([self.type isEqualToString:@"1"]){
        self.view_acceptanceBtn.hidden = YES;
    }
}


/**隐患信息*/
- (IBAction)clickInformation:(UIButton *)sender {
    [self.tableView beginUpdates];
    self.tableView.tableHeaderView.height = W(727);
    [self.tableView  endUpdates];
    
    [self.btn_information setTitleColor:[UIColor colorNamed:@"AccentColor"] forState:UIControlStateNormal];
    [self.btn_acceptance setTitleColor:[UIColor colorNamed:@"333Color"] forState:UIControlStateNormal];
    self.view_acceptance.hidden = YES;
    self.view_information.hidden = NO;
    if([self.type isEqualToString:@"1"]){
        self.view_acceptanceBtn.hidden = YES;
    }
    
}


/**通过*/
- (IBAction)clickThrough:(UIButton *)sender {
    [self acceptanceLookModel:@"3"];
}


/**退回*/
- (IBAction)clickBack:(UIButton *)sender {
    [AppDelegate addSubview:self.view_popupWindow];
//
}

- (IBAction)clickPopupBack:(UIButton *)sender {
    [self acceptanceLookModel:@"1"];
}

- (IBAction)clickPopupCancel:(UIButton *)sender {
    [self.view_popupWindow removeFromSuperview];
}

-(void)acceptanceLookModel:(NSString *)type{
    if ([type isEqualToString:@"1"]) {
        if ([self.tv_describe.text isEqualToString:@""]) {
            [CCToast showMessage:@"请添加隐患整改描述"];
            return;
        }
    }
    
    [SQAcceptanceLookModel getDataWithParams:@{@"hd_id":self.hd_id,
                                               @"hd_acceptor_id":UID,
                                               @"hd_acceptor_text":MName,
                                               @"hd_acceptor_company_id":MEnterpriseId,
                                               @"hd_acceptor_company_text":MEnterpriseText,
                                               @"hd_acceptance_remarks":self.tv_describe.text,
                                               @"hd_audit_status":type
        
    } WithCallBack:^(SQAcceptanceLookModel * data) {
       
        
        if ([type isEqualToString:@"2"]) {
            [CCToast showMessage:@"验收成功"];
        }else if([type isEqualToString:@"1"]){
            [self.view_popupWindow removeFromSuperview];
            [CCToast showMessage:@"退回成功"];
        }
        
        [[NSNotificationCenter defaultCenter] postNotificationName:@"acceptance" object:nil];
        
        [self.navigationController popViewControllerAnimated:YES];
    }];
    
}

#pragma mark -- 图片放大
- (IBAction)clickShowImage:(UIButton *)sender {

    if (sender.tag == 10) {
        if ([self haveStr:@[self.queryModel.data.hd_report_pic_one]]) {
            [SQShowImage showImage:self.img_picOne];
            
        }
        
    }else if (sender.tag == 20){
        if ([self haveStr:@[self.queryModel.data.hd_report_pic_two]]) {
            [SQShowImage showImage:self.img_picTwo];
        }
        
        
    }else if (sender.tag == 30){
        if ([self haveStr:@[self.queryModel.data.hd_report_pic_three]]) {
            [SQShowImage showImage:self.img_picThree];
        }
            
    }else  if (sender.tag == 40) {
        if ([self haveStr:@[self.queryModel.data.hd_rectification_pic_one]]) {
            [SQShowImage showImage:self.img_picAcceptanceOne];
            
        }
        
    }else if (sender.tag == 50){
        if ([self haveStr:@[self.queryModel.data.hd_rectification_pic_two]]) {
            [SQShowImage showImage:self.img_picAcceptanceTwo];
        }
        
        
    }else if (sender.tag == 60){
        if ([self haveStr:@[self.queryModel.data.hd_rectification_pic_three]]) {
            [SQShowImage showImage:self.img_picAcceptanceThree];
        }
            
    }
}

- (BOOL)haveStr:(NSArray<NSString *> *)strs{
    for (NSString *key in strs) {
        if (key.length > 0) {
            return YES;
        }
    }
    return NO;
}

@end
